package com.example.base.security.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.base.config.PageData;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.limit.IPRateLimit;
import com.example.base.security.domain.SysDept;
import com.example.base.security.domain.vo.SysDeptVo;
import com.example.base.security.service.SysDeptService;
import com.example.base.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author xiong
 * @since 2024-02-05
 */

@RestController
@Api(tags = {"部门表的服务类"})
@RequestMapping("/sys-dept")
public class SysDeptController {

        @Autowired
        private SysDeptService sysDeptService;

        @ApiOperation(value = "获取部门tree结构")
        @GetMapping("/gettree")
        public JSONObject getTreeListController(){
                List<SysDept> treeList=sysDeptService.treeList();
                return Result.successInfo(treeList);
        }


        @PostMapping("/saveorupdate")
        @IPRateLimit(name = "SysDept-saveorupdate",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "部门表的更新和新增接口",notes = "部门表的更新和新增接口")
        public JSONObject save(@RequestBody SysDeptVo sysDeptVo) throws GlobalExcetion {
                sysDeptService.saveOrUpdateSysDept(sysDeptVo);
                return Result.successInfo("保存成功");
        }

        @DeleteMapping("/{id}")
        @IPRateLimit(name = "SysDept-del",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "部门表的删除接口",notes = "部门表的删除接口")
        public JSONObject delete(@PathVariable("id") Serializable id) {
                boolean flag = sysDeptService.removeById(id);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @PostMapping("/del/batch")
        @IPRateLimit(name = "SysDept-delbatch",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "部门表的批量删除接口",notes = "部门表的批量删除接口")
        public JSONObject deleteBatch(@RequestBody List<Integer> ids){
                boolean flag =  sysDeptService.removeByIds(ids);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @GetMapping("/list")
        @IPRateLimit(name = "SysDept-getall",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "部门表的获取全部接口",notes = "部门表的获取全部接口")
        public JSONObject findAll() {
                List<SysDept> list= sysDeptService.list();
                return Result.successInfo(list);
        }

        @GetMapping("/{id}")
        @IPRateLimit(name = "SysDept-getid",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "部门表的获取一个接口",notes = "部门表的获取一个接口")
        public JSONObject findOne(@PathVariable("id") Serializable id) {
                SysDept data= sysDeptService.getById(id);
                return Result.successInfo(data);
        }

        @GetMapping("/page")
        @IPRateLimit(name = "SysDept-page",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "部门表的分页接口",notes = "部门表的分页接口")
        public JSONObject findPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
                QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
                IPage<SysDept> data=sysDeptService.page(new Page<>(page, pageSize),queryWrapper);
                PageData pageData=new PageData(data.getTotal(),page,pageSize,data.getRecords());
                return Result.successInfo(pageData);
        }
}

